<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	String etlUrl = (String) request.getAttribute("etlUrl");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<base href="<%=basePath%>">

	<title>个人ETL跑批</title>
	<%@include file="head.jsp"%>
</head>
<body>

<div id="datedialog" class="easyui-dialog" title="跑批设置" style="width:400px;height:200px;padding-left:30px;padding-top:50px" data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true">
	<form id="dateform" action="">
		时间参数：<input id="dateselect" type="text" class="easyui-datebox" required="required"><br/><br/>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="easyui-linkbutton" iconCls="icon-ok" onclick="excutePath()">执行</a>&nbsp;&nbsp;&nbsp;&nbsp;
		<a class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#datedialog').window('close')">取消</a>
	</form>
</div>

<div style="padding:5px;" id="tb">
	<a class="easyui-linkbutton" iconCls="icon-add" onclick="add()">手工录入</a>&nbsp;&nbsp;
	<a class="easyui-linkbutton" iconCls="icon-edit" onclick="edit()">修改</a>&nbsp;&nbsp;
	<a class="easyui-linkbutton" iconCls="icon-remove" onclick="del()">删除</a>&nbsp;&nbsp;
	<a class="easyui-linkbutton" iconCls="icon-ok" onclick="run()">跑批</a>&nbsp;&nbsp;
	所属系统：
	<select class="easyui-combobox" name="select" id="select"
			data-options="valueField:'subValue',textField:'subText',url:'getSystemName',prompt: '请选择系统类别',editable:false"  style="height: 23px; width: 140px" />
	</select>
	<a class="easyui-linkbutton" iconCls="icon-search" onclick="query()">查询</a>&nbsp;&nbsp;
</div>


<table id="dg" style="height: 400px;"></table>

<div style="visibility: hidden;">
	<div id="addDialog" class="easyui-dialog" data-options="iconCls:'icon-add',toolbar:'#tbForAdd'" style="width: 800px; height: 250px;top:100px;">
		<!--对话框的工具栏  -->
		<div id="tbForAdd" style="padding: 5px;height:28px;padding-top:5px">
			<a class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="InfoAdd()" id="addBtn">提交</a>
		</div>
		<form id="FormAdd" method="post">
			<table style="padding-left: 150px;padding-top:30px; padding-right: 30px">
				<tr>
					<td align="right">所属系统:</td>
					<td>
						<select class="easyui-combobox" name="subject" id="subject_a"
								data-options="valueField:'subName',textField:'subName',url:'getNameId',prompt: '请选择系统类别',required:true,editable:false"  style="height: 23px; width: 225px" />
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">任务名称:</td>
					<td>
						<input data-options="multiline:true,validType:'length[1,60]',required:true" class="easyui-validatebox" name="name" id="name_a" style="height: 23px; width: 225px" missingMessage="该项不能为空" />
					</td>
				</tr>
				<tr>
					<td align="right">任务说明:</td>
					<td><input data-options="multiline:true,validType:'length[1,60]'" class="easyui-validatebox" name="task" id="task_a" style="height: 23px; width: 225px" /></td>
				</tr>
				<tr>
					<td align="right">文件路径:</td>
					<td>
						<input data-options="multiline:true,validType:'length[1,60]',required:true" class="easyui-validatebox" name="odspath" id="odspath_a" style="height: 23px; width: 225px"  missingMessage="该项不能为空"/>
					</td>
					<input type="hidden" id="id_a" name="id"/>
				</tr>

				<%--<tr>
					<td align="right">时间日期:</td>
					<td>
						<input class="easyui-datebox" name="data_data" id="data_data_a" style="height: 23px; width: 225px" missingMessage="该项不能为空" editable="fasle" required="required" data-options="required:true"/>
					</td>
				</tr>
--%>
			</table>
		</form>
	</div>
</div>


<!--修改-->
<div style="visibility: hidden;">
	<div id="editDialog" class="easyui-dialog" data-options="iconCls:'icon-edit',toolbar:'#tbForEdit'" style="width: 800px; height: 250px;top:100px;">
		<!--对话框的工具栏  -->
		<div id="tbForEdit" style="padding: 5px;height:28px;padding-top:5px">
			<a class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="InfoEdit()" id="editBtn">提交</a>
		</div>
		<form id="FormEdit" method="post">
			<table style="padding-left: 150px;padding-top:30px; padding-right: 30px">
				<tr>
					<td align="right">所属系统:</td>
					<td>
						<select editable="fasle" class="easyui-combobox" name="subject" id="subject_e"
								data-options="valueField:'subName',textField:'subName',url:'getNameId',prompt: '请选择系统类别',required:true,editable:false" disabled="disabled"  style="height: 23px; width: 225px" />
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">任务名称:</td>
					<td>
						<input data-options="multiline:true,validType:'length[1,60]',required:true" class="easyui-validatebox" name="name" id="name_e" style="height: 23px; width: 225px" missingMessage="该项不能为空"/>
					</td>
				</tr>
				<tr>
					<td align="right">任务说明:</td>
					<td><input data-options="multiline:true,validType:'length[1,60]'" class="easyui-validatebox" name="task" id="task_e" style="height: 23px; width: 225px"/></td>
				</tr>
				<tr>
					<td align="right">文件路径:</td>
					<td><input data-options="multiline:true,validType:'length[1,60]',required:true"  class="easyui-validatebox" name="odspath" id="odspath_e" style="height: 23px; width: 225px"  missingMessage="该项不能为空"/>
						<input type="hidden" id="id_e" name="id"/>

					</td>
				</tr>
				<%--<tr>
					<td align="right">时间日期:</td>
					<td>
						<input class="easyui-datebox" name="data_data" id="data_data_e" style="height: 23px; width: 225px" missingMessage="该项不能为空" editable="fasle" data-options="required:true"/>
					</td>
				</tr>--%>

			</table>
		</form>
	</div>
</div>


<%--<div style="visibility: hidden;">--%>
<%--<div id="importExcel" class="easyui-dialog" style="width: 600px; height: 90px;padding: 8px 10px 6px 5px" title="数据导入" data-options="iconCls:'icon-save',closed:true">--%>
<%--文件导入:<input type="file" name="excelfile" id="excelfile" size="45" onchange="inexcel()"/>&nbsp;--%>
<%--<input type="button" id="importbtn" value="提交" size="60" onclick="excel()"/>--%>
<%--</div>--%>
<%--</div>--%>


<script type="text/javascript">

	$(function (){
		$('#editDialog').window('close');
		$('#addDialog').window('close');


		// createInit();
		// $('#importExcel').window('close');
		$("#dg").datagrid({
			loadMsg:'数据加载中，请稍后...',
			url:'PETLList?randSession=${authId}',
			// url:'getNameId',
			checkOnSelect:true,
			autoRowHeight:false,        //自动行高
			rownumbers:true,            //开启行号
			fitColumns:true,
			pagination:true,     //开启分页
			fit:true,
			toolbar:'#tb',
			pageSize:25,
			pageList:[25,50],
			columns:[[
				{field:'id',title:'编号',align:'center',width:80,checkbox: true},
				{field:'subject',title:'所属系统',align:'center',width:80},
				{field:'name',title:'任务名称',width:80,align:'center'},
				{field:'task',title:'任务说明',width:80,align:'center'},
				{field:'odspath',title:'文件路径',width:80,align:'center'},

				/*{field:'status',title:'跑批状态',width:80,align:'center'}*/
			]]
		});
	});

	$('#addDialog').dialog({
		onClose: function () {
			$('.validatebox-tip').remove();
		}
	});
	$('#editDialog').dialog({
		onClose: function () {
			$('.validatebox-tip').remove();
		}
	});


	function add(){
		//加载表单
		$("#FormAdd").form('clear');

		//最后打开窗口
		$("#addDialog").window({title: "跑批信息手工录入"});
		$('#addDialog').window('open');
		$('#addDialog').window({modal: true});
		$('#addBtn').linkbutton('enable'); // 可用（取消置灰）
	}


	//保存
	function InfoAdd(){
		var name = $('#name_a').val().trim();
		var task = $('#task_a').val().trim();
		var odspath = $('#odspath_a').val().trim();
		var subject = $('#subject_a').combobox("getValue");

		if ($('#addDialog').form('validate')) {
			if (!odspath.endWith(".bat") && !odspath.endWith(".kjb") && !odspath.endWith(".sh")&& !odspath.endWith(".ktr")){
				$.messager.alert('错误',"文件格式错误(支持.bat、.sh、.kjb、.ktr)");
				return;
			} else {
				$('#addBtn').linkbutton('disable'); // 不可用（置灰）
			}
			$.post('PETLAdd',{
						name : name,
						task : task,
						odspath : odspath,
						subject : subject,
						//data_data:data_data,
						randSession:"${authId}",
						dataType:'json',
						contentType:'application/x-www-form-urlencoded; charset=UTF-8',
					},
					function (data){
						if(data == '1'){
							$.messager.alert('','数据提交成功','info',function (r){
								$('#dg').datagrid('reload');
								$('#addDialog').window('close');
							});
						}else{
							$.messager.alert('','数据提交失败','error');
						}
					});
		}
	}

	String.prototype.endWith=function(endStr){
		var d=this.length-endStr.length;
		return (d>=0&&this.lastIndexOf(endStr)==d)
	}
	var getUrl = function(flie) {
		var url = ''
		if (window.createObjectURL!=undefined) { // basic
			url = window.createObjectURL(flie) ;
		}else if (window.webkitURL!=undefined) { // webkit or chrome
			url = window.webkitURL.createObjectURL(flie) ;
		}else if (window.URL!=undefined) { // mozilla(firefox)
			url = window.URL.createObjectURL(flie) ;
		}
		return url
	}

	function edit(){
		var rowData = $('#dg').datagrid('getSelections');
		if (rowData.length != 1) { //如果选中
			$.messager.alert("操作提示", "请选择一条数据进行修改操作！", 'error');
		}else{
			//加载表单
			$("#FormEdit").form('clear');
			$("#FormEdit").form('load',rowData[0]);

			//最后打开窗口
			$("#editDialog").window({title: "跑批信息修改"});
			$('#editDialog').window('open');
			$('#editDialog').window({modal: true});
			$('#editBtn').linkbutton('enable'); // 可用（取消置灰）
		}

	}


	function InfoEdit(){
		var rowData = $('#dg').datagrid('getSelected');
		var id = rowData.id;
		var name = $('#name_e').val().trim();
		var task = $('#task_e').val().trim();
		var odspath = $('#odspath_e').val().trim();
		var subject = $('#subject_e').combobox("getValue");

		// var subject = $('#subject').val().trim();
		// var data_data = $('#data_data_e').val().trim();
		//var data_data = $('#data_data_e').datebox('getValue');
		if ($('#FormEdit').form('validate')) {
			if (!odspath.endWith(".bat") && !odspath.endWith(".kjb") && !odspath.endWith(".sh")&& !odspath.endWith(".ktr")){
				$.messager.alert('错误',"文件格式错误(支持.bat、.sh、.kjb、.ktr)");
				return;
			} else {
				$('#editBtn').linkbutton('disable'); // 可用（取消置灰）
			}
			$.post('PETLEdit',{
						id  : id,
						name : name,
						task : task,
						odspath : odspath,
						subject : subject,
						//data_data:data_data,
						randSession:"${authId}",
						dataType:'json',
						contentType:'application/x-www-form-urlencoded; charset=UTF-8',
					},
					function (data){
						console.log(data);
						if(data == '1'){
							$.messager.alert('','数据提交成功','info',function (r){
								$('#dg').datagrid('reload');
								$('#editDialog').window('close');
							});
						}else{
							$.messager.alert('','数据提交失败','error');
						}
					});
		}
	}


	//删除
	function del() {
		var rowData = $('#dg').datagrid('getSelections');
		if (rowData.length > 0) { //如果选中
			var deldatas = $('#dg').datagrid('getSelections');
			var ids = '';
			for ( var dataIndex in deldatas) {
				ids = ids + deldatas[dataIndex].id+',';
			}
			ids = ids.substring(0,ids.length-1);
			$.messager.confirm("确认删除", "确认删除数据吗？", function(r) {
				if (r)
					$.post('PETLDelete', {
						ids : ids,
						"randSession":"${authId}",
					}, function(r) {
						$.messager.alert('提示',"删除成功",'info',function (r){
							$('#dg').datagrid('reload');
						});

					});

			});
		}else{
			$.messager.alert("操作提示", "请选择一条数据进行删除操作！", 'error');
		}

	}

	function run() {
		var rows = $('#dg').datagrid('getSelections');
		if (rows.length!=1) {
			$.messager.alert('操作提示','请选择一条数据进行跑批','error');
		}else {
			$('#datedialog').window('open');
		}
	}


	function excutePath(){
		$('#datedialog').window('close');
		var rows = $('#dg').datagrid('getSelections');
		var  url =rows[0].odspath;
		var dataDate = $('#dateselect').datebox('getValue');
		console.log(dataDate);
		$.messager.progress({
			title:'请稍等',
			msg:'执行跑批中......'
		});
		$.ajax({
			url : 'runPath',
			type : 'GET', //GET
			contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
			data : {"pathname" : url.replace(new RegExp("/","gm"),"\\"),"type": "1","dataDate":dataDate,"randSession":"${authId}"},
			dataType : 'json', //返回的数据格式：json/xml/html/script/jsonp/text
			success : function(data) {
				$.messager.progress("close");
				if (data.code == 200) {
					$.messager.alert('执行完毕',data.desc,'info');
				} else if (data.code == 400) {
					$.messager.alert('执行完毕',data.desc,'info');
					window.location = "DownloadLog";
				} else if (data.code == 404 || data.code == 501) {
					$.messager.alert('执行失败',data.desc,'error');
				}
			}
		});
	}

	//点击查询按钮
	function query(){
		var select = $("#select").combobox("getValue");
		$("#dg").datagrid("load",{select:select});
	}
</script>
</body>
</html>